package com.xlh.adapter;

import java.util.ArrayList;
import java.util.List;

import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.bean.TitleBean;
import com.xlh.interf.IAdapter;
import com.xlh.outside.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GridViewAndListViewAdapter extends BaseAdapter implements IAdapter {

	private List<TitleBean> data = new ArrayList<TitleBean>();
	private LayoutInflater mInflater;
	private Context context;

	private int gridConums;

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}




	//左边按钮还是右边按钮 1 右边 2 左边
	private int type = 1;

	public int getGridConums() {
		return gridConums;
	}

	public GridViewAndListViewAdapter setGridConums(int gridConums) {
		this.gridConums = gridConums;
		return this;
	}

	public int getParentWidth() {
		return parentWidth;
	}

	public GridViewAndListViewAdapter setParentWidth(int parentWidth) {
		this.parentWidth = parentWidth;
		return this;
	}

	private int parentWidth;


	public GridViewAndListViewAdapter(Context context, List<TitleBean> data) {
		// TODO Auto-generated constructor stub
		this.context = context;
		mInflater = LayoutInflater.from(context);
		this.data = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data == null ? 0 : data.size();
	}
	public TitleBean getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder ;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.grid_view_item, parent, false);
			holder = new ViewHolder();
			holder.tvName = (TextView) convertView.findViewById(R.id.title_text);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}


		//获取VIEW高度
		int height = 42;//默认40高度

		//只有左边按钮才会用宽度设定
		int width = 50;//默认40高度
		ViewGroup.LayoutParams parentLparam  = convertView.getLayoutParams();



		ViewGroup.LayoutParams vLparam = holder.tvName.getLayoutParams();

		vLparam.height = FUti.dip2px(context,height);

//		if(this.getType()==2){
//			vLparam.width = FUti.dip2px(context,width);
//			parentLparam.width = FUti.dip2px(context,width);
//		}

		TitleBean titleBean = data.get(position);



		holder.tvName.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor() ));
		if(gridConums==1){


			//只有一列 内容左对齐 且居中
			holder.tvName.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);

			holder.tvName.setPadding(FUti.dip2px(convertView.getContext(),5),
					FUti.dip2px(convertView.getContext(),5),
					FUti.dip2px(convertView.getContext(),5),
					FUti.dip2px(convertView.getContext(),5));
			//单行时 列高度 又内容调整
		}
		holder.tvName.setText(titleBean.getSpanned());

//		GradientDrawable drawable = (GradientDrawable) holder.tvName.getBackground();
//		//设置边框
//		drawable.setStroke(FUti.dip2px(convertView.getContext(),1), Color.parseColor(Constant.getAppColor().getButBorderColor()));
//		//设置背景
//		drawable.setColor(Color.parseColor(Constant.getAppColor().getButBackColor()));



		return convertView;
	}



	private class ViewHolder {
		
		private TextView tvName;
	}
	


	public void setData(List<TitleBean> mTitleData) {
		// TODO Auto-generated method stub
		if(mTitleData != null) {
			this.data = mTitleData;
		}
		this.notifyDataSetChanged();
	}

	public void clear() {
		// TODO Auto-generated method stub
		if(data != null) {
			data.clear();
		}
		this.notifyDataSetChanged();
	}






	@Override
	public <T> void addData(T data) {
		if(this.data != null) {
			this.data.add((TitleBean)data);
		}
	}

	@Override
	public <T> IAdapter setDatas(List<T> datas) {
		this.data = (List<TitleBean>)datas;
		return this;
	}

	public void update(){
		this.notifyDataSetChanged();
	}
	
}
